package com.xyz;

import java.util.List;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import com.xyz.StudentJDBCTemplate;

public class MainApp {
    public static void main(String[] args) {
        // 创建上下文对象，来进行依赖注入
        ApplicationContext context =
                new ClassPathXmlApplicationContext("Beans.xml");
        // 创建自定义的 JDBCTemplate 对象
        StudentDAO studentJDBCTemplate =
                (StudentJDBCTemplate)context.getBean("studentJDBCTemplate");
        // 先进行数据库出入操作
        System.out.println("------插入三条数据--------" );
        studentJDBCTemplate.create("小编", 11);
        studentJDBCTemplate.create("小白", 20);
        studentJDBCTemplate.create("嘿嘿", 15);
        System.out.println("------列出所有学生信息--------" );
        List<Student> students = studentJDBCTemplate.listStudents();
        for (Student record : students) {
            System.out.print("ID : " + record.getId() );
            System.out.print(", 姓名 : " + record.getName() );
            System.out.println(", 年龄 : " + record.getAge());
        }
        System.out.println("----更新 id = 2 的学生年龄为 99-----" );
        studentJDBCTemplate.update(2, 99);
        System.out.println("----查询 ID = 2 的学生信息-----" );
        Student student = studentJDBCTemplate.getStudent(2);
        System.out.print("ID : " + student.getId() );
        System.out.print(", 姓名 : " + student.getName() );
        System.out.println(", 年龄 : " + student.getAge());
    }
}